/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov;

import java.io.Serializable;

/**
 * The state-option-value function that stores the estimated value (Q-value) of 
 * state-option pairs.
 * 
 * 
 * 
 * @author Matthijs Snel
 *
 */
public interface QFunction extends Serializable {

	/**
	 * Adds the provided delta to the current value of the specified state-option pair.
	 * 
	 * @param s the state of the state-option pair of which to update the value.
	 * @param act the option of the state-option pair of which to update the value.
	 * @param delta the delta that will be added to the current value.
	 */
	public <T> void update(State<T> s, Option opt, double delta);
	
	/**
	 * Adds the provided delta
	 * to the current value of the state-option pair(s) of the last state that was
	 * queried using {@link #getValue(State)} or {@link #getValue(State, Option)}.
	 * 
	 * @param delta the delta that will be added to the current value.
	 */
	public void update(double delta);
	
	/**
	 * Adds the provided delta, multiplied by the learning rate alpha,
	 * to the current value of the state-option pair(s) of the next-to-last state
	 * that was queried using {@link #getValue(State)} or 
	 * {@link #getValue(State, Option)} (optional operation).
	 * 
	 * @param delta the delta that will be added to the current value.
	 */
	public void updatePrevious(double delta);
	
	/**
	 * Gets the value of the specified state-option pair (optional operation). The decision of what to return if
	 * the value of the state-option pair is unknown (the pair hasn't been encountered before)
	 * is left up to the implementing classes.
	 * 
	 * This operation is optional since function approximators will usually only be able
	 * to implement either this method or {@link #getValue(State)}. At least one of these
	 * methods should be implemented.
	 * 
	 * @param s the state of the state-option pair of which to retrieve the value.
	 * @param act the option of the state-option pair of which to retrieve the value.
	 * @return the value of the state-option pair.
	 * @throws UnsupportedOperationException if the operation is not supported.
	 */
	public <T> double getValue(State<T> s, Option opt);
	
	/**
	 * Gets the values of all options for this state (optional operation). The decision of what to return if
	 * the values of the options for this state are unknown (the state hasn't been encountered before)
	 * is left up to the implementing classes.
	 * 
	 * This operation is optional since function approximators will usually only be able
	 * to implement either this method or {@link #getValue(State, Option)}. At least one of these
	 * methods should be implemented.
	 * 
	 * @param s the state of which to retrieve the option values.
	 * @return an array containing values of the options for this state
	 * @throws UnsupportedOperationException if the operation is not supported.
	 */
	public <T> double[] getValues(State<T> s);
	
	/**
	 * Tell the value function what the last chosen option is. This is used for the
	 * {@link #update(double)} and {@link #updatePrevious(double)} methods, and is normally
	 * assumed to be the last queried option using {@link #getValue(State, Option)}.
	 * 
	 * This method provides a way for setting the last option explicitly; it also needs to
	 * be used when the query method used is {@link #getValue(State)}, as in this case
	 * there is no way for the value function to know what option was selected, and it 
	 * simply assumes it to be the first option.
	 * 
	 * @param a the last option taken.
	 */
	public void setLastOption(Option o);
	
	/**
	 * Sets the value of the specified state-option pair (optional operation). Any stored
	 * value for this pair will be replaced with the new one.
	 * 
	 * This is an optional operation as function approximators will not be able to do this
	 * directly.
	 * 
	 * @param s the state of the state-option pair of which to set the value.
	 * @param act the option of the state-option pair of which to set the value.
	 * @param value the value of the state.
	 * @throws UnsupportedOperationException if the operation is not supported.
	 */
	public <T> void setValue(State<T> s, Option opt, double value);
	
	/**
	 * Called at the beginning of an experiment.
	 */
	public void init();
	
	/**
	 * Called whenever the learning scenario is reset without clearing the learned values,
	 * e.g. at the beginning of a new episode in episodic tasks.
	 */
	public void reset();
	
	/**
	 * 
	 * @return single parameter representing {@code gamma*lambda} for eligibility traces.
	 */
	public double getTraceDecay();
	
	/**
	 * Single parameter representing {@code gamma*lambda} for eligibility traces. Set to 0
	 * for learning without eligibility traces.
	 * 
	 * This method is on the value function instead of on the agent since
	 * 1. For function approximators the function gradient is needed to update the traces,
	 * which the agent doesn't have access to.
	 * 2. For tabular functions, multiple updates need to take place (for states with
	 * positive trace), whereas for FAs, only a single update needs to take place. By
	 * placing the trace updates within the value function, only a single agent implementation
	 * is needed.
	 */
	public void setTraceDecay(double decay);
	
	/**
	 * Initialise the function to these values.
	 * @param initValue
	 */
	public void setInitValues(double initValue);
}
